﻿$(function () {
    (function ($) {

        $.fn.autoResize = function (options) {

            // Just some abstracted details,
            // to make plugin users happy:
            var settings = $.extend({
                onResize: function () { },
                animate: true,
                animateDuration: 150,
                animateCallback: function () { },
                extraSpace: 20,
                limit: 1000
            }, options);

            // Only textarea's auto-resize:
            this.filter('textarea').each(function () {

                // Get rid of scrollbars and disable WebKit resizing:
                var textarea = $(this).css({ resize: 'none', 'overflow-y': 'hidden' }),

                    // Cache original height, for use later:
                    origHeight = textarea.height(),

                    // Need clone of textarea, hidden off screen:
                    clone = (function () {

                        // Properties which may effect space taken up by chracters:
                        var props = ['height', 'width', 'lineHeight', 'textDecoration', 'letterSpacing'],
                            propOb = {};

                        // Create object of styles to apply:
                        $.each(props, function (i, prop) {
                            propOb[prop] = textarea.css(prop);
                        });

                        // Clone the actual textarea removing unique properties
                        // and insert before original textarea:
                        return textarea.clone().removeAttr('id').removeAttr('name').css({
                            position: 'absolute',
                            top: 0,
                            left: -9999
                        }).css(propOb).attr('tabIndex', '-1').insertBefore(textarea);

                    })(),
                    lastScrollTop = null,
                    updateSize = function () {

                        // Prepare the clone:
                        clone.height(0).val($(this).val()).scrollTop(10000);

                        // Find the height of text:
                        var scrollTop = Math.max(clone.scrollTop(), origHeight) + settings.extraSpace,
                            toChange = $(this).add(clone);

                        // Don't do anything if scrollTip hasen't changed:
                        if (lastScrollTop === scrollTop) { return; }
                        lastScrollTop = scrollTop;

                        // Check for limit:
                        if (scrollTop >= settings.limit) {
                            $(this).css('overflow-y', '');
                            return;
                        }
                        // Fire off callback:
                        settings.onResize.call(this);

                        // Either animate or directly apply height:
                        settings.animate && textarea.css('display') === 'block' ?
                            toChange.stop().animate({ height: scrollTop }, settings.animateDuration, settings.animateCallback)
                            : toChange.height(scrollTop);
                    };

                // Bind namespaced handlers to appropriate events:
                textarea
                    .unbind('.dynSiz')
                    .bind('keyup.dynSiz', updateSize)
                    .bind('keydown.dynSiz', updateSize)
                    .bind('change.dynSiz', updateSize);

            });

            // Chain:
            return this;

        };



    })(jQuery);

    $('textarea').autoResize();
    $("#XemThuNoiDung").hide();
    $("#HuyXemThu").hide();
    $.fx.speeds._default = 1000;
    $("#linkurl").dialog({
        autoOpen: false,
        show: "blind",
        hide: "explode",
        buttons: {
            Add: function () {
                var url = $("#textlinkurl").val();
                var mota = $("#text1linkurl").val();
                document.getElementById("TextArea1").value = document.getElementById("TextArea1").value +
                "<a href='" + url + "'>" + mota + "</a>";
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });
    $("#imgurl").dialog({
        autoOpen: false,
        show: "blind",
        hide: "explode",
        buttons: {
            Add: function () {
                var url = $("#textimgurl").val();
                document.getElementById("TextArea1").value = document.getElementById("TextArea1").value +
                "<img src='" + url + "' class='picture'></img>";
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });
    $("#TextQuote").dialog({
        autoOpen: false,
        show: "blind",
        hide: "explode",
        buttons: {
            Add: function () {
                var url = $("#ContentQuote").val();
                document.getElementById("TextArea1").value = document.getElementById("TextArea1").value +
                '<blockquote>" ' + url + ' "</blockquote>';
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });
    $("#bold")
    .button()
    .click(function () {
        document.getElementById("TextArea1").value = document.getElementById("TextArea1").value + "<b></b>";
    });
    $("#italic")
    .button()
    .click(function () {
        document.getElementById("TextArea1").value = document.getElementById("TextArea1").value + "<i></i>";
    });
    $("#underline")
    .button()
    .click(function () {
        document.getElementById("TextArea1").value = document.getElementById("TextArea1").value + "<u></u>";
    });
    $("#removeformat")
    .button()
    .click(function () {
        var a = document.getElementById("TextArea1").value;
        a = a.replace('<b>', '');
        a = a.replace('</b>', '');
        a = a.replace('<u>', '');
        a = a.replace('</u>', '');
        a = a.replace('<i>', '');
        a = a.replace('</i>', '');
        document.getElementById("TextArea1").value = a;
    });
    $("#createlink")
    .button()
    .click(function () {
        $("#linkurl").dialog("open");
    });
    $("#insertimage")
    .button()
    .click(function () {
        $("#imgurl").dialog("open");
    });
    $("#quote")
    .button()
    .click(function () {
        $("#TextQuote").dialog("open");
    });
    $("#XemThu")
    .button()
    .click(function () {
        $("#NoiDung").hide();
        $("#HuyXemThu").show();
        var a = document.getElementById("TextArea1").value;
        $("#XemThuNoiDung").show();
        $("#XemThuNoiDung").append(a);
        $("#XemThu").hide();
    });
    $("#HuyXemThu")
    .button()
    .click(function () {
        $("#XemThuNoiDung").empty();
        $("#XemThuNoiDung").hide();
        $("#NoiDung").show();
        $("#XemThu").show();
        $("#HuyXemThu").hide();
    });
    $("#Br")
   .button()
   .click(function () {
       document.getElementById("TextArea1").value = document.getElementById("TextArea1").value +"<Br>";
   });
});